import sys
from ApiBase import apiBase

api = apiBase.argv(1,'http://8.130.66.117:8082/88api/langgraph/echoA.hpl')
code = apiBase.argv(2,"请计算1+1")
sys_prompt = apiBase.argv(3,"你是数学专家")
usr_prompt = apiBase.argv(4,"根据表达式计算结果")
fix_prompt = apiBase.argv(5,"修改里面的错误")

# 修复3次,如果不成功就退出
def fix(sys_prompt,usr_prompt,code):
    error = None
    prompt=usr_prompt
    for i in range(2):
        # 如果有错误,就进行修复
        out = apiBase.llm_chat(sys_prompt,prompt,code,error)
        ret = apiBase.call_api(api,out,True)
        if ret is None:
            return "The format returned by the API is incorrect"      
        error = ""
        for item in ret['data']:
            if 'error' in item:
                error = error + item['error']
        if error == "":
            return out
        error=apiBase.decode64(error)
        prompt=fix_prompt
    return "Fix unsuccessful"

#修复成功,返回正确值
try:
    ret=fix(sys_prompt,usr_prompt,code)
    print(ret)
except Exception as e:
    print(f"Exception:{e}") 
finally:    
    apiBase.close()


